﻿
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using NJ.Common.Ioc;
using NJ.EntityFramework;

namespace NJ.Service
{
    public class BaseService : IDependency
    {
        protected NJContext dbContext = new NJContext();
        /// <summary>
        /// 添加一个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Entity"></param>
        /// <returns></returns>
        public void Add<T>(T Entity) where T : class
        {
            dbContext.Set<T>().Add(Entity);
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities"></param>
        public void AddBatch<T>(IEnumerable<T> entities) where T : class
        {
            dbContext.Set<T>().AddRange(entities);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities"></param>
        public void DeleteBatch<T>(IEnumerable<T> entities) where T : class
        {
            dbContext.Set<T>().RemoveRange(entities);
        }
        /// <summary>
        /// 删除单个对象的实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Entity">实体对象</param>
        /// <returns></returns>
        public void Delete<T>(T Entity) where T : class
        {
            dbContext.Set<T>().Remove(Entity);
        }
        /// <summary>
        /// 进行修改单个实体对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Entity">实体对象</param>
        /// <returns></returns>
        public void Update<T>(T Entity) where T : class
        {
            dbContext.Entry(Entity).State = EntityState.Modified;
        }

        /// <summary>
        /// 查询单个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">主键ID</param>
        /// <returns></returns>
        public async Task<T> FindAsync<T>(params object[] key) where T : class
        {
            return await dbContext.Set<T>().FindAsync(key);
        }

        /// <summary>
        /// 获取第一个值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async Task<T> FirstOrDefaultAsync<T>() where T : class
        {
            return await dbContext.Set<T>().FirstOrDefaultAsync();
        }

        /// <summary>
        /// 获取全部数据的列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public IQueryable<T> GetAll<T>(Expression<Func<T, bool>> expression) where T : class
        {
            return dbContext.Set<T>().Where(expression);
        }

        /// <summary>
        /// 获取全部数据的列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IQueryable<T> GetAll<T>() where T : class
        {
            return dbContext.Set<T>();
        }
        /// <summary>
        /// 保存更改
        /// </summary>
        /// <returns></returns>
        public async Task<int> SaveChangesAsync()
        {
            var result = await dbContext.SaveChangesAsync();

            return result;
        }

        /// <summary>
        /// 释放缓存
        /// </summary>
        public void Dispose()
        {
            dbContext.Dispose();
        }
    }
}
